#!/usr/bin/python
#
# extract-objs - Given a set of static libraries, this script extracts 
# their objects to the specified directory. Usage:
# extract-objs <lib1.a> <lib2.a> ... <libN.a> <dir>

import sys
if len(sys.argv) == 1:
    print 'extract-objs: Extract objects from a list of static libs to a directory.'
    print 'usage: extract-objs <lib1.a> <lib2.a> ... <libN.a> <dir>'
    exit(0)
numLibs = len(sys.argv) - 2
if numLibs == 0:
    print 'extract-objs: At least one library must be specified.'
libs = sys.argv[1:-1]
dest = sys.argv[-1]
import os, os.path, stat
if os.path.exists(dest) and not stat.S_ISDIR(os.stat(dest).st_mode):
    print 'Destination directory is a file! Please specify an existing'
    print 'directory or a new one to be created.'
elif not os.path.exists(dest):
    os.mkdir(dest)

cwd = os.getcwd()
os.chdir(dest)
for lib in libs:
    os.system('ar -x %s'%lib) 
os.chdir(cwd)
